package com.dhecp.core.utils;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PdfUtil {
	
	public static void main(String[] args) throws Exception {
		
//		String dir = "D://data";
//		String pdfFileName = "/商品标.pdf";
//		generateGoods(dir, pdfFileName, null, 10);
		
		String dir = "D://data";
		String pdfFileName = "/采购入库单.pdf";
		List<StockInDto.RowDto> rows = new ArrayList<StockInDto.RowDto>();
		{
			rows.add(new StockInDto.RowDto("PD21515433", "蝴蝶嫁接睫毛隔离片 10件装", "5"));
			rows.add(new StockInDto.RowDto("PD21515433", "蝴蝶嫁接睫毛隔离片 10件装", "5"));
			rows.add(new StockInDto.RowDto("PD21515433", "蝴蝶嫁接睫毛隔离片 10件装", "5"));
		}
		List<StockInDto.BomDto> boms = new ArrayList<StockInDto.BomDto>();
		{
			boms.add(new StockInDto.BomDto("PD21515433", "蝴蝶嫁接睫毛隔离片 10件装", "5"));
			boms.add(new StockInDto.BomDto("PD21515433", "蝴蝶嫁接睫毛隔离片 10件装", "5"));
		}
		StockInDto dto = new StockInDto("CGRK-1000-1000", "CGDD-1000-1000", "GSBM-001", "思路", "ZZFA-1000-1000",
				"", "PD220630002974/蝴蝶嫁接睫毛隔离片 10件装",
				"http://cdn.mini-ease.com/profile/images/20221228/919f38bb70c2a6134ff9c7227985b647.jpg", rows, boms);
		generateStockIn(dir, pdfFileName, dto);
		
//		generatePeiHuo(dir, pdfFileName);
//		generateZuzhuang(dir, pdfFileName, null);
	}
	
	public static Image generateBarcodeImg(String dir, String imgFileName) throws FileNotFoundException, DocumentException {
		File file = new File(dir + imgFileName);
		Document document = new Document();
		PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(file));
		PdfContentByte cb = new PdfContentByte(pdfWriter);

	    Barcode128 code128 = new Barcode128();
	    code128.setCode("X0010CFKUL");
	    code128.setCodeType(Barcode128.CODE128);
	    Image code128Image = code128.createImageWithBarcode(cb, null, null);
	    code128Image.setAlignment(Image.ALIGN_CENTER);
	    code128Image.scalePercent(300);
	    
	    return code128Image;
	}
	
	public static void generateGoods(String dir, String pdfFileName, GoodsDto dto, Integer num) {
		

		Rectangle rec = new RectangleReadOnly(500, 300);
		
		// 1.新建document对象
		Document document = new Document(rec);// 建立一个Document对象
		document.setMargins(0, 0, 15f, 0);
//		document.setPageCount(num);//设置生成pdf页数
		try {
			// 2.建立一个书写器(Writer)与document对象关联
//			File file = new File("D://data/PDF商品标.pdf");
			File file = new File(dir + pdfFileName);
			file.createNewFile();
			FileOutputStream fos = new FileOutputStream(file);
			PdfWriter pdfWriter = PdfWriter.getInstance(document, fos);
			
			// 3.打开文档
			document.open();
//			PdfContentByte cb = pdfWriter.getDirectContent();
			
			//4.生成PDF
			for (int i = 0; i < num; i++) {
				document.setPageCount(i);//设置生成pdf页数
//				//4.生成PDF
				new PdfUtil().generateGoodsPDF2(document, pdfWriter, dto);
			}
			
			// 5.关闭文档
			document.close();
			System.out.println("PageSize---" + document.getPageSize());
			System.out.println("PageNumber---" + document.getPageNumber());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void generateGoods3(String dir, String pdfFileName, GoodsPDF3Dto dto) {

		try {
			// 1.新建document对象
			Document document = null;// 建立一个Document对象
			{
				Rectangle rec = new RectangleReadOnly(210, 297);
				document = new Document(rec);
				document.setMargins(5, 5, 5, 5);
				document.setPageCount(1);//设置生成pdf页数
			}
			// 2.建立一个书写器(Writer)与document对象关联
//			File file = new File("D://data/PDF采购入库.pdf");
			File file = new File(dir + pdfFileName);
			file.createNewFile();
			PdfWriter.getInstance(document, new FileOutputStream(file));
			// 3.打开文档
			document.open();
			//4.生成PDF
			new PdfUtil().generateGoodsPDF3(document, dto);
			// 5.关闭文档
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void generateStockIn(String dir, String pdfFileName, StockInDto dto) {
		
		try {
			// 1.新建document对象
			Document document = null;// 建立一个Document对象
			{
				Rectangle rec = new RectangleReadOnly(500, 500);
				document = new Document(rec);
				document.setMargins(0, 0, 15f, 0);
				document.setPageCount(1);//设置生成pdf页数
			}
			// 2.建立一个书写器(Writer)与document对象关联
//			File file = new File("D://data/PDF采购入库.pdf");
			File file = new File(dir + pdfFileName);
			file.createNewFile();
			PdfWriter.getInstance(document, new FileOutputStream(file));
			// 3.打开文档
			document.open();
			//4.生成PDF第一页
			new PdfUtil().generateStockIn1(document, dto);
			//4.生成PDF第二页
			new PdfUtil().generateStockIn2(document, dto);
			// 5.关闭文档
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @param dir 目录
	 * @param pdfFileName PDF的文件名
	 */
	public static void generatePeiHuo(String dir, String pdfFileName, PeihuoDto dto) {
		
		Rectangle rec = new RectangleReadOnly(420, 600);
		
		try {
			// 1.新建document对象
			Document document = new Document(rec);// 建立一个Document对象
			document.setMargins(0, 0, 15f, 0);
			document.setPageCount(1);//设置生成pdf页数
			// 2.建立一个书写器(Writer)与document对象关联
//			File file = new File("D://data/PDF配货清单.pdf");
			File file = new File(dir + pdfFileName);
			file.createNewFile();
			PdfWriter.getInstance(document, new FileOutputStream(file));
			// 3.打开文档
			document.open();
			
			//4.生成PDF
			for (int i = 0; i < dto.itemArr.size(); i++) {
				
				document.newPage();
//				document.setPageCount(i);//设置生成pdf页数
				//4.生成PDF
				new PdfUtil().generatePeiHuo(document, dto.itemArr.get(i));
			}
			// 5.关闭文档
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @param dir 目录
	 * @param pdfFileName PDF的文件名
	 */
	public static void generateOuterXiangBiao(String dir, String pdfFileName, OuterXiangBiaoDto dto) {
		
		Rectangle rec = new RectangleReadOnly(420, 600);
		
		try {
			// 1.新建document对象
			Document document = new Document(rec);// 建立一个Document对象
			document.setMargins(0, 0, 15f, 0);
			document.setPageCount(1);//设置生成pdf页数
			// 2.建立一个书写器(Writer)与document对象关联
//			File file = new File("D://data/PDF配货清单.pdf");
			File file = new File(dir + pdfFileName);
			file.createNewFile();
			PdfWriter.getInstance(document, new FileOutputStream(file));
			// 3.打开文档
			document.open();
			
			//4.生成PDF
			{
				document.newPage();
//				document.setPageCount(i);//设置生成pdf页数
				//4.生成PDF
				new PdfUtil().generateOuterXiangBiao(document, dto);
			}
			// 5.关闭文档
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @param dir 目录
	 * @param pdfFileName PDF的文件名
	 */
	public static void generateZuzhuang(String dir, String pdfFileName, ZuzhuangDto dto) {
		
		Rectangle rec = new RectangleReadOnly(800, 500);
		
		try {
			// 1.新建document对象
			Document document = new Document(rec);// 建立一个Document对象
			document.setMargins(0, 0, 15f, 0);
			document.setPageCount(1);//设置生成pdf页数
			// 2.建立一个书写器(Writer)与document对象关联
//			File file = new File("D://data/PDF配货清单.pdf");
			File file = new File(dir + pdfFileName);
			file.createNewFile();
			PdfWriter.getInstance(document, new FileOutputStream(file));
			// 3.打开文档
			document.open();
			//4.生成PDF
			new PdfUtil().generateZuzhuang(document, dto);
			// 5.关闭文档
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @param dir 目录
	 * @param pdfFileName PDF的文件名
	 */
	public static void generateFinancePdf(String dir, String pdfFileName, BaoXiaoDto dto) {
		
		Rectangle rec = new RectangleReadOnly(420, 600);
		
		try {
			// 1.新建document对象
			Document document = new Document(rec);// 建立一个Document对象
			document.setMargins(0, 0, 15f, 0);
			document.setPageCount(1);//设置生成pdf页数
			// 2.建立一个书写器(Writer)与document对象关联
//			File file = new File("D://data/PDF配货清单.pdf");
			File file = new File(dir + pdfFileName);
			file.createNewFile();
			PdfWriter.getInstance(document, new FileOutputStream(file));
			// 3.打开文档
			document.open();
			
			//4.生成PDF
			document.newPage();
//			document.setPageCount(i);//设置生成pdf页数
			new PdfUtil().generateFinance(document, dto);
			
			// 5.关闭文档
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @param dir 目录
	 * @param pdfFileName PDF的文件名
	 */
	public static void generateOfflinePaymentPdf(String dir, String pdfFileName, OfflinePaymentDto dto) {
		
		Rectangle rec = new RectangleReadOnly(420, 600);
		
		try {
			// 1.新建document对象
			Document document = new Document(rec);// 建立一个Document对象
			document.setMargins(0, 0, 15f, 0);
			document.setPageCount(1);//设置生成pdf页数
			// 2.建立一个书写器(Writer)与document对象关联
//			File file = new File("D://data/PDF配货清单.pdf");
			File file = new File(dir + pdfFileName);
			file.createNewFile();
			PdfWriter.getInstance(document, new FileOutputStream(file));
			// 3.打开文档
			document.open();
			
			//4.生成PDF
			document.newPage();
//			document.setPageCount(i);//设置生成pdf页数
			new PdfUtil().generateOfflinePayment(document, dto);
			
			// 5.关闭文档
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// 定义全局的字体静态变量
	private static BaseFont bfChinese = createFontLocal("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

	private static Font textfont4 = new Font(bfChinese, 4, Font.NORMAL);
	private static Font textfont6 = new Font(bfChinese, 6, Font.NORMAL);
	private static Font textfont8 = new Font(bfChinese, 8, Font.NORMAL);
	private static Font textfont10 = new Font(bfChinese, 10, Font.NORMAL);
//	private static Font textfont11 = new Font(bfChinese, 11, Font.NORMAL);
	private static Font textfont12 = new Font(bfChinese, 12, Font.NORMAL);
	private static Font textfont14 = new Font(bfChinese, 14, Font.NORMAL);
	private static Font textfont16 = new Font(bfChinese, 16, Font.NORMAL);
	private static Font textfont18 = new Font(bfChinese, 18, Font.NORMAL);
	
	private static Font headfont;
	private static Font keyfont;
	private static Font textfont;

	// 静态代码块
	static {
		try {
			// 不同字体（这里定义为同一种字体：包含不同字号、不同style）
			BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
			headfont = new Font(bfChinese, 20, Font.NORMAL);
			keyfont = new Font(bfChinese, 10, Font.BOLD);
			textfont = new Font(bfChinese, 15, Font.NORMAL);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 本地创建字体对象
	 */
	public static BaseFont createFontLocal(String name, String encoding, boolean embedded) {
		try {
			return BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		} catch (DocumentException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	// 生成PDF文件
	public void generateGoodsPDF(Document document, Image code128Image, GoodsDto dto) throws Exception {
		
	// 空白表格
	PdfPTable tableBlank = createBlankTable(15.0f);
	document.add(tableBlank);

	 // 添加图片
//	Image image = Image.getInstance(dto.getImgBarcode());
//	Image image = Image.getInstance("D://data/barcode.png");
//	image.setAlignment(Image.ALIGN_CENTER);
//	image.scalePercent(150); //依照比例缩放
//	image.setSpacingBefore(0f);
//	document.add(image);
	
//    document.add(code128Image);
	
	// 空白表格
	document.add(tableBlank);
	
	 // 表格
	 PdfPTable table2;
	 {
		 table2 = createTable(new float[]{180, 360}, 0, 500);
		 table2.addCell(createCell("NEW", headfont, Element.ALIGN_CENTER, 0));
		 table2.addCell(createCell("Hair accessories Hair accessories", textfont, Element.ALIGN_LEFT, 0));
//		 table.addCell(createCell(dto.getNameEn(), textfont, Element.ALIGN_LEFT, 0));
		 

		 table2.addCell(createCell("", textfont, Element.ALIGN_CENTER, 0));
		 table2.addCell(createCell("枪黑色按扣 12个装 21mm", textfont, Element.ALIGN_LEFT, 0));
//		 table.addCell(createCell(dto.getNameCn(), textfont, Element.ALIGN_LEFT, 0));
	 }
	 table2.setSpacingBefore(12f);
	 
	 document.add(table2);
	}
	
	// 生成PDF文件
	public void generateGoodsPDF2(Document document, PdfWriter pdfWriter, GoodsDto dto) throws Exception {
		
		Font headfont2 = new Font(bfChinese, 30, Font.NORMAL);
	
		Font textfont2 = new Font(bfChinese, 25, Font.NORMAL);
	 // 表格
	 PdfPTable table;
	 {
		 table = createTable(new float[]{100, 200}, 0, 500);

//		 table.addCell(createCellBarcode(pdfWriter, "X0010CFKUL", 1, 2, 0));
		 table.addCell(createCellBarcode(pdfWriter, dto.getImgBarcode(), 1, 2, 0));
		 
		 table.addCell(createCell("NEW", headfont2, Element.ALIGN_CENTER, 0));
//		 table.addCell(createCell("Hair accessories Hair accessories", textfont2, Element.ALIGN_LEFT, 0));
		 table.addCell(createCell(dto.getNameEn(), textfont2, Element.ALIGN_LEFT, 0));

		 table.addCell(createCell("", textfont2, Element.ALIGN_CENTER, 0));
//		 table.addCell(createCell("枪黑色按扣 12个装 21mm", textfont2, Element.ALIGN_LEFT, 0));
		 table.addCell(createCell(dto.getNameCn(), textfont2, Element.ALIGN_LEFT, 0));
	 }
	 
	 document.add(table);
	}
	
//	// 生成组装清单PDF
//	public void generateGoodsPDF3(Document document, GoodsPDF3Dto dto) throws Exception {
//	
//		// 表格
//		PdfPTable table;
//		{
//			table = createTable(new float[]{50, 5, 20, 50, 5, 20, 50, 5}, 0, 205);
//			
//			Image image = Image.getInstance("D:/data/test/test.png");
//			image.setAlignment(Image.ALIGN_CENTER);
//			image.setAlignment(Image.ALIGN_MIDDLE);
//			image.scalePercent(150); //依照比例缩放
//			image.setSpacingBefore(0f);
//			image.setBottom(0f);
//			for (int i = 0; i < 9; i++) {
//				table.addCell(createCellImg(image, 0));
//				table.addCell(createCell("", textfont10, Element.ALIGN_CENTER, 0));
//				table.addCell(createCell("", textfont10, Element.ALIGN_CENTER, 0));
//				table.addCell(createCellImg(image, 0));
//				table.addCell(createCell("", textfont10, Element.ALIGN_CENTER, 0));
//				table.addCell(createCell("", textfont10, Element.ALIGN_CENTER, 0));
//				table.addCell(createCellImg(image, 0));
//				table.addCell(createCell("", textfont10, Element.ALIGN_CENTER, 0));
//				
//				
//				table.addCell(createCell(dto.getProductCode(), textfont4, Element.ALIGN_CENTER, 2, 0));
//				table.addCell(createCell("", textfont4, Element.ALIGN_CENTER, 0));
//				table.addCell(createCell(dto.getProductCode(), textfont4, Element.ALIGN_CENTER, 2, 0));
//				table.addCell(createCell("", textfont4, Element.ALIGN_CENTER, 0));
//				table.addCell(createCell(dto.getProductCode(), textfont4, Element.ALIGN_CENTER, 2, 0));
//				
//				table.addCell(createCell(dto.getProductNameCn(), textfont4, Element.ALIGN_CENTER, 3, 0));
//				table.addCell(createCell(dto.getProductNameCn(), textfont4, Element.ALIGN_CENTER, 3, 0));
//				table.addCell(createCell(dto.getProductNameCn(), textfont4, Element.ALIGN_CENTER, 2, 0));
//			}
//			
//		}
//		document.add(table);
//	}
	@Data
	@AllArgsConstructor
	public static class GoodsPDF3Dto{
		private String productCode; // 
		private String productNameCn; // 
		private String productNameEn; // 
	}

	// 生成组装清单PDF
	public void generateGoodsPDF3(Document document, GoodsPDF3Dto dto) throws Exception {

		// 表格
		PdfPTable table;
		{
			table = createTable(new float[]{50, 5, 20, 50, 5, 20, 50, 5}, 0, 205);

			Image image = Image.getInstance("D:/data/test/test.png");
			image.setAlignment(Image.ALIGN_CENTER);
			image.setAlignment(Image.ALIGN_MIDDLE);
			image.scalePercent(150); //依照比例缩放
			image.setSpacingBefore(0f);
			image.setBottom(0f);
			for (int i = 0; i < 9; i++) {
				table.addCell(createCellImg(image, 0));
				table.addCell(createCell("", textfont10, Element.ALIGN_CENTER, 0));
				table.addCell(createCell("", textfont10, Element.ALIGN_CENTER, 0));
				table.addCell(createCellImg(image, 0));
				table.addCell(createCell("", textfont10, Element.ALIGN_CENTER, 0));
				table.addCell(createCell("", textfont10, Element.ALIGN_CENTER, 0));
				table.addCell(createCellImg(image, 0));
				table.addCell(createCell("", textfont10, Element.ALIGN_CENTER, 0));


				table.addCell(createCell(dto.getProductCode(), textfont4, Element.ALIGN_CENTER, 2, 0));
				table.addCell(createCell("", textfont4, Element.ALIGN_CENTER, 0));
				table.addCell(createCell(dto.getProductCode(), textfont4, Element.ALIGN_CENTER, 2, 0));
				table.addCell(createCell("", textfont4, Element.ALIGN_CENTER, 0));
				table.addCell(createCell(dto.getProductCode(), textfont4, Element.ALIGN_CENTER, 2, 0));

				table.addCell(createCell(dto.getProductNameCn(), textfont4, Element.ALIGN_CENTER, 3, 0));
				table.addCell(createCell(dto.getProductNameCn(), textfont4, Element.ALIGN_CENTER, 3, 0));
				table.addCell(createCell(dto.getProductNameCn(), textfont4, Element.ALIGN_CENTER, 2, 0));
			}

		}
		document.add(table);
	}

	@Data
	@AllArgsConstructor
	public static class GoodsDto{
		private String nameEn; // 商品英文名
		private String nameCn; // 商品中文名
		private String imgBarcode; // 条形码图片
		private Integer num; // 要打印的数量
	}

	@Data
	@AllArgsConstructor
	public static class PeihuoGoodsTagDto{
		
		private List<RowDto> rows;

		@Data
		@AllArgsConstructor
		public static class RowDto{
			
			private String code; // 商品编码
			private String name; // 商品名称
			private String fnsku; // FNSKU
			private Integer numPlanDelever; // 计划发货数量
		}
	}
	
	// 生成采购入库单PDF
	public void generateStockIn1(Document document, StockInDto dto) throws Exception {
		document.setPageCount(0);

		Image image = Image.getInstance(dto.getImg());
		image.setAlignment(Image.ALIGN_CENTER);
		image.setAlignment(Image.ALIGN_MIDDLE);
		image.scalePercent(150); //依照比例缩放
		image.setSpacingBefore(0f);
		image.setBottom(0f);
		document.add(image);
	}
	
	// 生成采购入库单PDF
	public void generateStockIn2(Document document, StockInDto dto) throws Exception {
		document.newPage();
	
		// 表格
		PdfPTable table1;
		{
			table1 = createTable(new float[]{160, 320}, 0, 480);
			
			table1.addCell(createCell("采购入库单：", textfont18, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell(dto.getCodeCgrk(), textfont18, Element.ALIGN_LEFT, 0));
			
			table1.addCell(createCell("采购订单：", textfont18, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell(dto.getCodeCgdd(), textfont18, Element.ALIGN_LEFT, 0));
			
			table1.addCell(createCell("公司编码：", textfont18, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell(dto.getCodeCompany(), textfont18, Element.ALIGN_LEFT, 0));
			
			table1.addCell(createCell("公司简称：", textfont18, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell(dto.getNameCompany(), textfont18, Element.ALIGN_LEFT, 0));
			
			table1.addCell(createCell("组装方案：", textfont18, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell(dto.getNameZz(), textfont18, Element.ALIGN_LEFT, 0));
			
			table1.addCell(createCell("组装方式：", textfont18, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell(dto.getNameWay(), textfont18, Element.ALIGN_LEFT, 0));
			
			table1.addCell(createCell("商品信息：", textfont18, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell(dto.getInfoGoods(), textfont18, Element.ALIGN_LEFT, 0));
		}
		
		// 空白表格
		PdfPTable tableBlank = createBlankTable(5.0f);
	
		// 表格2
		PdfPTable table2;
		{
			table2 = createTable(new float[]{140, 240, 100}, 1, 480);
			
			table2.addCell(createCell("货件清单", textfont18, Element.ALIGN_LEFT, 3, 0));
			
			table2.addCell(createCell("商品编码", textfont16, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("商品名称", textfont16, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("采购数量", textfont16, Element.ALIGN_CENTER, 1));
			
			for(StockInDto.RowDto row : dto.getRows()) {
				table2.addCell(createCell(row.getCodeGoods(), textfont16, Element.ALIGN_LEFT, 1));
				table2.addCell(createCell(row.getNameGoods(), textfont16, Element.ALIGN_LEFT, 1));
				table2.addCell(createCell(row.getNum(), textfont16, Element.ALIGN_CENTER, 1));
			}
			
		}
	
		// 表格3
		PdfPTable table3;
		{
			table3 = createTable(new float[]{140, 240, 100}, 1, 480);
			
			table3.addCell(createCell("BOM", textfont18, Element.ALIGN_LEFT, 3, 0));
			
			table3.addCell(createCell("子件编码", textfont16, Element.ALIGN_CENTER, 1));
			table3.addCell(createCell("子件名称", textfont16, Element.ALIGN_CENTER, 1));
			table3.addCell(createCell("子件数量", textfont16, Element.ALIGN_CENTER, 1));
			
			for(StockInDto.BomDto bom : dto.getBoms()) {
				table3.addCell(createCell(bom.getCodeGoods(), textfont16, Element.ALIGN_LEFT, 1));
				table3.addCell(createCell(bom.getNameGoods(), textfont16, Element.ALIGN_LEFT, 1));
				table3.addCell(createCell(bom.getNum(), textfont16, Element.ALIGN_CENTER, 1));
			}
			
		}
		document.add(table1);
		document.add(tableBlank);
		document.add(table2);
		document.add(tableBlank);
		document.add(table3);
	}

	@Data
	@AllArgsConstructor
	public static class StockInDto{
		private String codeCgrk; // 采购入库单号
		private String codeCgdd; // 采购订单号
		private String codeCompany; // 公司编码
		private String nameCompany; // 公司简称
		private String nameZz; // 组装方案
		private String nameWay; // 组装方式
		private String infoGoods; // 商品信息
		private String img; // 商品图片
		private List<RowDto> rows; // 货件清单数组
		private List<BomDto> boms; // BOM子件数组
		
		@Data
		@AllArgsConstructor
		public static class RowDto {
			private String codeGoods; // 商品编码
			private String nameGoods; // 商品名称
			private String num; // 采购数量
		}
		
		@Data
		@AllArgsConstructor
		public static class BomDto {
			private String codeGoods; // 商品编码
			private String nameGoods; // 商品名称
			private String num; // 采购数量
		}
	}
	
	// 生成配货清单PDF
	public void generatePeiHuo(Document document, PeihuoDto.ItemDto itemDto) throws Exception {
	
		// 表格
		PdfPTable table1;
		{
			table1 = createTable(new float[]{50, 100, 50, 100}, 0, 400);
			
			table1.addCell(createCell("配货清单：", textfont10, Element.ALIGN_LEFT, 4, 0));
			table1.addCell(createCell("头程计划单：", textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell(itemDto.getCodeFbaPlanOrder(), textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("物流方式：", textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell(itemDto.getNameWLFS(), textfont8, Element.ALIGN_LEFT, 0));
			
			table1.addCell(createCell("公司编码：", textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell(itemDto.getCodeCompany(), textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("公司简称：", textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell(itemDto.getNameCompany(), textfont8, Element.ALIGN_LEFT, 0));
			
			table1.addCell(createCell("预估重量：", textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell(itemDto.getWeightPredict(), textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("备注：", textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell(itemDto.getRemark(), textfont8, Element.ALIGN_LEFT, 0));
			
			table1.addCell(createCell("货件创建方式：", textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell(itemDto.getCreationMethod(), textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("", textfont8, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("", textfont8, Element.ALIGN_LEFT, 0));
		}
		
		// 空白表格
		PdfPTable tableBlank = createBlankTable(5.0f);
	
		// 表格2
		PdfPTable table2;
		{
			table2 = createTable(new float[]{10, 30, 40, 50, 40, 20, 15, 20, 40, 40}, 1, 400);

			table2.addCell(createCell("明细", textfont10, Element.ALIGN_LEFT, 10, 0));
			
			table2.addCell(createCell("序号", textfont8, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("图片", textfont8, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("商品编码", textfont8, Element.ALIGN_LEFT, 1));
			table2.addCell(createCell("商品名称", textfont8, Element.ALIGN_LEFT, 1));
			table2.addCell(createCell("FNSKU", textfont8, Element.ALIGN_LEFT, 1));
			table2.addCell(createCell("计划发货数量", textfont8, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("剩余库存", textfont8, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("货位", textfont8, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("透明标记编码", textfont8, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("装箱情况", textfont8, Element.ALIGN_CENTER, 1));
			
			int i = 1;
			for(PeihuoDto.ItemDto.RowDto row : itemDto.getRows()) {
				table2.addCell(createCell("" + i, textfont6, Element.ALIGN_CENTER, 1));
				
				Image image = null;
				try {
					image = Image.getInstance(row.getImgGoods());
				} catch (Exception e) {
					image = Image.getInstance("https://erptest.mini-ease.com/profile/white_blank.png");
				}
				image.setAlignment(Image.ALIGN_CENTER);
				image.scalePercent(20); //依照比例缩放
				image.setSpacingBefore(0f);
				table2.addCell(image);
				
				table2.addCell(createCell(row.getCodeGoods(), textfont6, Element.ALIGN_LEFT, 1));
				table2.addCell(createCell(row.getNameGoods(), textfont6, Element.ALIGN_LEFT, 1));
				table2.addCell(createCell(row.getFnsku(), textfont6, Element.ALIGN_LEFT, 1));
				table2.addCell(createCell(row.getNumPlanDeliver(), textfont6, Element.ALIGN_CENTER, 1));
				table2.addCell(createCell(row.getNumStockLeft(), textfont6, Element.ALIGN_CENTER, 1));
				table2.addCell(createCell(row.getLocationGoods(), textfont6, Element.ALIGN_CENTER, 1));
				table2.addCell(createCell(row.getSignName(), textfont6, Element.ALIGN_CENTER, 1));
				table2.addCell(createCell("", textfont6, Element.ALIGN_CENTER, 1)); // 装箱情况
				
				i++;
			}
			
		}
		document.add(table1);
		document.add(tableBlank);
		document.add(table2);
	}

	@Data
	@AllArgsConstructor
	public static class PeihuoDto{
		private List<ItemDto> itemArr; // 配货清单数组
		@Data
		@AllArgsConstructor
		public static class ItemDto{
			private String codeFbaPlanOrder; // 头程计划单号
			private String codeCompany; // 公司编码
			private String weightPredict; // 预估重量
			private String nameCompany; // 公司简称
			private String nameWLFS; // 物流方式名称
			private String remark; // 备注
			private String creationMethod; // 货件创建方式
			private List<RowDto> rows; // 明细
			
			@Data
			@AllArgsConstructor
			public static class RowDto {
				private String codeGoods; // 商品编码
				private String nameGoods; // 商品名称
				private String fnsku; // FNSKU
				private String numPlanDeliver; // 计划发货数量
				private String locationGoods; // 货位
				private String locationStock; // 库区
				private String locationName; // 仓库名
				private String imgGoods; // 商品图片
				private String numStockLeft; // 剩余库存
				private String signName; // 透明标记（是/否）
			}
		}
	}
	
	// 生成配货清单PDF
	public void generateOuterXiangBiao(Document document, OuterXiangBiaoDto dto) throws Exception {
	
		// 表格
		PdfPTable table;
		{
			table = createTable(new float[]{60, 120, 200}, 0, 380);
			
			table.addCell(createCell("运营公司简称：", textfont8, Element.ALIGN_LEFT, 0));
			table.addCell(createCell(dto.getCompanyShortName(), textfont8, Element.ALIGN_LEFT, 0));
			table.addCell(createCell("", textfont8, Element.ALIGN_LEFT, 0));
			
			table.addCell(createCell("物流方式：", textfont8, Element.ALIGN_LEFT, 0));
			table.addCell(createCell(dto.getNameWLFS(), textfont8, Element.ALIGN_LEFT, 0));
			table.addCell(createCell("", textfont8, Element.ALIGN_LEFT, 0));
			
			table.addCell(createCell("箱重：", textfont8, Element.ALIGN_LEFT, 0));
			table.addCell(createCell(dto.getWeight(), textfont8, Element.ALIGN_LEFT, 0));
			table.addCell(createCell("", textfont8, Element.ALIGN_LEFT, 0));
		}
		document.add(table);
	}

	@Data
	@AllArgsConstructor
	public static class OuterXiangBiaoDto{
		private String companyShortName; // 运营公司简称
		private String nameWLFS; // 物流方式名称
		private String weight; // 每箱的重量（按KG显示，方便美国箱标的核对）
	}
	
	// 生成组装清单PDF
	public void generateZuzhuang(Document document, ZuzhuangDto dto) throws Exception {
	
		// 表格
		PdfPTable table;
		{
			table = createTable(new float[]{50, 30, 50, 50, 100, 50, 20, 50, 100, 20, 20}, 1, 700);
			
			// 第一行需要跨列数量
			table.addCell(createCell("组装清单", textfont14, Element.ALIGN_LEFT, 11, 0));
			
			table.addCell(createCell("组装单号", textfont12, Element.ALIGN_CENTER, 1));
			table.addCell(createCell("公司编码", textfont12, Element.ALIGN_CENTER, 1));
			table.addCell(createCell("公司简称", textfont12, Element.ALIGN_CENTER, 1));
			table.addCell(createCell("母件编码", textfont12, Element.ALIGN_CENTER, 1));
			table.addCell(createCell("母件名称", textfont12, Element.ALIGN_CENTER, 1));
			table.addCell(createCell("母件尺寸", textfont12, Element.ALIGN_CENTER, 1));
			table.addCell(createCell("母件数量", textfont12, Element.ALIGN_CENTER, 1));
			table.addCell(createCell("子件编码", textfont12, Element.ALIGN_CENTER, 1));
			table.addCell(createCell("子件名称", textfont12, Element.ALIGN_CENTER, 1));
			table.addCell(createCell("采购数量", textfont12, Element.ALIGN_CENTER, 1));
			table.addCell(createCell("货位", textfont12, Element.ALIGN_CENTER, 1));
			
			for (ZuzhuangDto.RowDto row : dto.getRows()) {
				table.addCell(createCell(row.getCode(), textfont10, Element.ALIGN_CENTER, 1));
				
				table.addCell(createCell(row.getCodeCompany(), textfont10, Element.ALIGN_CENTER, 1));
				table.addCell(createCell(row.getNameCompany(), textfont10, Element.ALIGN_CENTER, 1));
				table.addCell(createCell(row.getPcodeGoods(), textfont10, Element.ALIGN_CENTER, 1));
				table.addCell(createCell(row.getPnameGoods(), textfont10, Element.ALIGN_CENTER, 1));
				table.addCell(createCell(row.getSize(), textfont10, Element.ALIGN_CENTER, 1));
				table.addCell(createCell(row.getPnum(), textfont10, Element.ALIGN_CENTER, 1));
				table.addCell(createCell(row.getCodeGoods(), textfont10, Element.ALIGN_CENTER, 1));
				table.addCell(createCell(row.getNameGoods(), textfont10, Element.ALIGN_CENTER, 1));
				table.addCell(createCell(row.getNum(), textfont10, Element.ALIGN_CENTER, 1));
				table.addCell(createCell(row.getLocationName(), textfont10, Element.ALIGN_CENTER, 1));
				
			}
			
		}
		document.add(table);
	}

	@Data
	@AllArgsConstructor
	public static class ZuzhuangDto{
		private List<RowDto> rows; // 明细
		
		@Data
		@AllArgsConstructor
		public static class RowDto {
			private String code; // 组装单号
			private String codeCompany; // 公司编码
			private String nameCompany; // 公司简称
			private String codeGoods; // 商品编码
			private String nameGoods; // 商品名称
			private String size; // 商品尺寸（长*宽*高）
			private String num; // 数量
			private String locationName; // 货位
			private String pcodeGoods; // 母件编码
			private String pnameGoods; // 母件名称
			private String pnum; // 母件数量
		}
	}
	
	/**
	 *  生成PDF：职能费用申请单/合作商付款/运营费用申请
	 */
	public void generateFinance(Document document, BaoXiaoDto dto) throws Exception {
	
		// 表格
		PdfPTable table1;
		{
			table1 = createTable(new float[]{80, 60, 100, 60, 50}, 1, 400);
			
			table1.addCell(createCell(dto.getTitle(), textfont14, Element.ALIGN_CENTER, 5, 0));
			table1.addCell(createCell(dto.getCreateCompany(), textfont8, Element.ALIGN_LEFT, 3, 0));
			table1.addCell(createCell("申请时间：" + dto.getCreateTime(), textfont8, Element.ALIGN_LEFT, 2, 0));

			table1.addCell(createCell("单据编号：", textfont8, Element.ALIGN_CENTER, 1));
			table1.addCell(createCell(dto.getCode(), textfont8, Element.ALIGN_CENTER, 4, 1));

			table1.addCell(createCell("申请人：", textfont8, Element.ALIGN_CENTER, 1));
			table1.addCell(createCell(dto.getApplyUser(), textfont8, Element.ALIGN_CENTER, 4, 1));

			table1.addCell(createCell("申请部门：", textfont8, Element.ALIGN_CENTER, 1));
			table1.addCell(createCell(dto.getApplyDept(), textfont8, Element.ALIGN_CENTER, 4, 1));

			table1.addCell(createCell("收款账号：", textfont8, Element.ALIGN_CENTER, 1));
			table1.addCell(createCell(dto.getPayAccount(), textfont8, Element.ALIGN_CENTER, 4, 1));

			table1.addCell(createCell("付款类型：", textfont8, Element.ALIGN_CENTER, 1));
			table1.addCell(createCell(dto.getPayType(), textfont8, Element.ALIGN_CENTER, 4, 1));

			table1.addCell(createCell("付款金额：", textfont8, Element.ALIGN_CENTER, 1));
			table1.addCell(createCell(dto.getPayAmount(), textfont8, Element.ALIGN_CENTER, 4, 1));

			table1.addCell(createCell("付款说明：", textfont8, Element.ALIGN_CENTER, 1));
			table1.addCell(createCell(dto.getRemark(), textfont8, Element.ALIGN_CENTER, 4, 1));
			
			List<BaoXiaoDto.DetailDto> detailArr = dto.getDetailArr();
			if (detailArr != null && detailArr.size() > 0) {

				table1.addCell(createCell("报销明细", textfont8, Element.ALIGN_LEFT, 5, 0));
				
				table1.addCell(createCell("工号及姓名", textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell("运营公司", textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell("用途", textfont8, Element.ALIGN_CENTER, 2, 1));
				table1.addCell(createCell("报销金额", textfont8, Element.ALIGN_CENTER, 1));
				
				for(BaoXiaoDto.DetailDto detail : detailArr) {

					table1.addCell(createCell(detail.getUser(), textfont8, Element.ALIGN_CENTER, 1));
					table1.addCell(createCell(detail.getOperatorCompanay(), textfont8, Element.ALIGN_CENTER, 1));
					table1.addCell(createCell(detail.getRemark(), textfont8, Element.ALIGN_CENTER, 2, 1));
					table1.addCell(createCell(detail.getAmount(), textfont8, Element.ALIGN_CENTER, 1));
				}
				
			}

			table1.addCell(createCell("审核明细", textfont8, Element.ALIGN_LEFT, 5, 0));
			{

				table1.addCell(createCell("处理时间", textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell("处理人", textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell("处理结果", textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell("处理意见", textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell("附件", textfont8, Element.ALIGN_CENTER, 1));
			}
			
			for(BaoXiaoDto.RowDto row : dto.getRows()) {

				table1.addCell(createCell(row.getOpeTime(), textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell(row.getOpeUser(), textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell(row.getOpeResult(), textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell(row.getOpeOpinion(), textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell(row.getFileName(), textfont8, Element.ALIGN_CENTER, 1));
			}
			
			{
				table1.addCell(createCell("打印时间：" + dto.getPrintTime(), textfont8, Element.ALIGN_LEFT, 2, 0));
				table1.addCell(createCell("打印人：" + dto.getPrintUser() + "   " + "打印人部门：" + dto.getPrintDept(), textfont8, Element.ALIGN_LEFT, 3, 0));
			}
		}
		document.add(table1);
	}
	@Data
	@AllArgsConstructor
	public static class BaoXiaoDto{
		private String title; // 单据标题
		private String createCompany; // 制单公司
		private String createTime; // 申请时间
		private String code; // 单据编号
		private String applyUser; // 申请人
		private String applyDept; // 申请部门
		private String payType; // 支付类型
		private String payAccount; // 支付账号
		private String payAmount; // 支付金额
		private String remark; // 付款说明
		private String printTime; // 打印时间
		private String printUser; // 打印人
		private String printDept; // 打印部门
		private List<DetailDto> detailArr; // 报销明细数组
		private List<RowDto> rows; // 处理明细
		
		@Data
		@AllArgsConstructor
		public static class DetailDto {
			private String user; // 账户所属
			private String operatorCompanay; // 运营公司
			private String remark; // 用途
			private String amount; // 报销金额
		}
		
		@Data
		@AllArgsConstructor
		public static class RowDto {
			private String opeTime; // 处理时间
			private String opeUser; // 商品名称
			private String opeResult; // FNSKU
			private String opeOpinion; // 计划发货数量
			private String fileName; // 附件名
		}
	}
	
	/**
	 *  生成PDF：线下付款单
	 */
	public void generateOfflinePayment(Document document, OfflinePaymentDto dto) throws Exception {
	
		// 表格
		PdfPTable table1;
		{
			table1 = createTable(new float[]{80, 60, 100, 60, 50}, 1, 400);
			
			table1.addCell(createCell(dto.getTitle(), textfont14, Element.ALIGN_CENTER, 5, 0));
			table1.addCell(createCell(dto.getCreateCompany(), textfont8, Element.ALIGN_LEFT, 3, 0));
			table1.addCell(createCell("申请时间：" + dto.getCreateTime(), textfont8, Element.ALIGN_LEFT, 2, 0));

			table1.addCell(createCell("单据编号：", textfont8, Element.ALIGN_CENTER, 1));
			table1.addCell(createCell(dto.getCode(), textfont8, Element.ALIGN_CENTER, 4, 1));

			table1.addCell(createCell("申请人：", textfont8, Element.ALIGN_CENTER, 1));
			table1.addCell(createCell(dto.getApplyUser(), textfont8, Element.ALIGN_CENTER, 4, 1));

			table1.addCell(createCell("申请部门：", textfont8, Element.ALIGN_CENTER, 1));
			table1.addCell(createCell(dto.getApplyDept(), textfont8, Element.ALIGN_CENTER, 4, 1));

			table1.addCell(createCell("收款账号：", textfont8, Element.ALIGN_CENTER, 1));
			table1.addCell(createCell(dto.getPayAccount(), textfont8, Element.ALIGN_CENTER, 4, 1));

			table1.addCell(createCell("付款类型：", textfont8, Element.ALIGN_CENTER, 1));
			table1.addCell(createCell(dto.getPayType(), textfont8, Element.ALIGN_CENTER, 4, 1));

			table1.addCell(createCell("付款金额：", textfont8, Element.ALIGN_CENTER, 1));
			table1.addCell(createCell(dto.getPayAmount(), textfont8, Element.ALIGN_CENTER, 4, 1));

			table1.addCell(createCell("付款说明：", textfont8, Element.ALIGN_CENTER, 1));
			table1.addCell(createCell(dto.getRemark(), textfont8, Element.ALIGN_CENTER, 4, 1));
			
			List<OfflinePaymentDto.DetailDto> detailArr = dto.getDetailArr();
			if (detailArr != null && detailArr.size() > 0) {

				table1.addCell(createCell("报销明细", textfont8, Element.ALIGN_LEFT, 5, 0));
				
				table1.addCell(createCell("工号及姓名", textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell("运营公司", textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell("用途", textfont8, Element.ALIGN_CENTER, 2, 1));
				table1.addCell(createCell("报销金额", textfont8, Element.ALIGN_CENTER, 1));
				
				for(OfflinePaymentDto.DetailDto detail : detailArr) {

					table1.addCell(createCell(detail.getUser(), textfont8, Element.ALIGN_CENTER, 1));
					table1.addCell(createCell(detail.getOperatorCompanay(), textfont8, Element.ALIGN_CENTER, 1));
					table1.addCell(createCell(detail.getRemark(), textfont8, Element.ALIGN_CENTER, 2, 1));
					table1.addCell(createCell(detail.getAmount(), textfont8, Element.ALIGN_CENTER, 1));
				}
				
			}

			table1.addCell(createCell("审核明细", textfont8, Element.ALIGN_LEFT, 5, 0));
			{

				table1.addCell(createCell("处理时间", textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell("处理人", textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell("处理结果", textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell("处理意见", textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell("附件", textfont8, Element.ALIGN_CENTER, 1));
			}
			
			for(OfflinePaymentDto.RowDto row : dto.getRows()) {

				table1.addCell(createCell(row.getOpeTime(), textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell(row.getOpeUser(), textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell(row.getOpeResult(), textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell(row.getOpeOpinion(), textfont8, Element.ALIGN_CENTER, 1));
				table1.addCell(createCell(row.getFileName(), textfont8, Element.ALIGN_CENTER, 1));
			}
			
			{
				table1.addCell(createCell("打印时间：" + dto.getPrintTime(), textfont8, Element.ALIGN_LEFT, 2, 0));
				table1.addCell(createCell("打印人：" + dto.getPrintUser() + "   " + "打印人部门：" + dto.getPrintDept(), textfont8, Element.ALIGN_LEFT, 3, 0));
			}
		}
		document.add(table1);
	}
	@Data
	@AllArgsConstructor
	public static class OfflinePaymentDto{
		private String title; // 单据标题
		private String createCompany; // 制单公司
		private String createTime; // 申请时间
		private String code; // 单据编号
		private String applyUser; // 申请人
		private String applyDept; // 申请部门
		private String payType; // 付款类型
		private String payAccount; // 收款账号
		private String payAmount; // 付款金额
		private String remark; // 付款说明
		private String printTime; // 打印时间
		private String printUser; // 打印人
		private String printDept; // 打印部门
		private List<DetailDto> detailArr; // 报销明细数组
		private List<RowDto> rows; // 处理明细
		
		@Data
		@AllArgsConstructor
		public static class DetailDto {
			private String user; // 账户所属
			private String operatorCompanay; // 运营公司
			private String remark; // 用途
			private String amount; // 报销金额
		}
		
		@Data
		@AllArgsConstructor
		public static class RowDto {
			private String opeTime; // 处理时间
			private String opeUser; // 商品名称
			private String opeResult; // FNSKU
			private String opeOpinion; // 计划发货数量
			private String fileName; // 附件名
		}
	}
	
	/**
	 * 创建指定列宽、列数的表格
	 */
	public static PdfPTable createTable(float[] widths, Integer border, Integer totalWidth) {
	    PdfPTable table = new PdfPTable(widths);
	    try {
	        table.setTotalWidth(totalWidth);
	        table.setLockedWidth(true);
	        table.setHorizontalAlignment(Element.ALIGN_CENTER);
	        table.getDefaultCell().setBorder(border);
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    return table;
	}

	/**
	 * 创建空白的表格
	 */
	public PdfPTable createBlankTable(float spacing) {
	    PdfPTable table = new PdfPTable(1);
	    table.getDefaultCell().setBorder(0);
	    table.addCell(createCell("", keyfont, 0));
	    table.setSpacingAfter(spacing);
	    table.setSpacingBefore(spacing);
	    return table;
	}

	/**
	 * 创建单元格(指定字体)
	 */
	public PdfPCell createCell(String value, Font font, Integer border) {
		PdfPCell cell = new PdfPCell();
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setPhrase(new Phrase(value, font));
		cell.setBorder(border);
		if (border > 0) {
			cell = handelBorder(cell, 0.1f);
		} else {
			cell.disableBorderSide(15);//全部隐藏
		}
//		cell.disableBorderSide(side); // 1隐上2隐下3隐上下4隐左8隐右
		return cell;
	}
	
	/**
	 * 创建单元格（指定字体、水平..）
	 */
	public static PdfPCell createCell(String value, Font font, int align, Integer border) {
	    PdfPCell cell = new PdfPCell();
	    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	    cell.setHorizontalAlignment(align);
	    cell.setPhrase(new Phrase(value, font));
		cell.setBorder(border);
		if (border > 0) {
			cell = handelBorder(cell, 0.1f);
		} else {
			cell.disableBorderSide(15);//全部隐藏
		}
	    return cell;
	}

	/**
	 * 创建单元格（指定字体、水平居..、单元格跨x列合并）
	 *
	 * @param value
	 * @param font
	 * @param align
	 * @param colspan
	 * @return
	 */
	public PdfPCell createCell(String value, Font font, int align, int colspan, Integer border) {
	    PdfPCell cell = new PdfPCell();
	    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	    cell.setHorizontalAlignment(align);
	    cell.setColspan(colspan);
	    cell.setPhrase(new Phrase(value, font));
		cell.setBorder(border);
		if (border > 0) {
			cell = handelBorder(cell, 0.1f);
		} else {
			cell.disableBorderSide(15);//全部隐藏
		}
	    return cell;
	}

	/**
	 * 创建单元格（指定字体、水平居..、单元格跨x列合并、设置单元格内边距）
	 *
	 * @param value
	 * @param font
	 * @param align
	 * @param colspan
	 * @param boderFlag
	 * @return
	 */
	public PdfPCell createCell(String value, Font font, int align, int colspan, boolean boderFlag, Integer border) {
	    PdfPCell cell = new PdfPCell();
	    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	    cell.setHorizontalAlignment(align);
	    cell.setColspan(colspan);
	    cell.setPhrase(new Phrase(value, font));
	    cell.setPadding(3.0f);
	    if (!boderFlag) {
	        cell.setPaddingTop(15.0f);
	        cell.setPaddingBottom(8.0f);
	    } else if (boderFlag) {
	        cell.setPaddingTop(0.0f);
	        cell.setPaddingBottom(15.0f);
	    }
		cell.setBorder(border);
		if (border > 0) {
			cell = handelBorder(cell, 0.1f);
		} else {
			cell.disableBorderSide(15);//全部隐藏
		}
	    return cell;
	}

	/**
	 * 创建单元格（指定字体、水平..、边框宽度：0表示无边框、内边距）
	 *
	 * @param value
	 * @param font
	 * @param align
	 * @param borderWidth
	 * @param paddingSize
	 * @param flag
	 * @return
	 */
	public PdfPCell createCell(String value, Font font, int align, float[] borderWidth, float[] paddingSize, boolean flag, Integer border) {
	    PdfPCell cell = new PdfPCell();
	    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	    cell.setHorizontalAlignment(align);
	    cell.setPhrase(new Phrase(value, font));
	    cell.setBorderWidthLeft(borderWidth[0]);
	    cell.setBorderWidthRight(borderWidth[1]);
	    cell.setBorderWidthTop(borderWidth[2]);
	    cell.setBorderWidthBottom(borderWidth[3]);
	    cell.setPaddingTop(paddingSize[0]);
	    cell.setPaddingBottom(paddingSize[1]);
	    if (flag) {
	        cell.setColspan(2);
	    }
		cell.setBorder(border);
		if (border > 0) {
			cell = handelBorder(cell, 0.1f);
		} else {
			cell.disableBorderSide(15);//全部隐藏
		}
	    return cell;
	}
	
	/*
	 * 创建单元格(指定字体)
	 */
	public PdfPCell createCellImg(Image image, Integer border) {
		PdfPCell cell = new PdfPCell();
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setImage(image);
		cell.setBorder(border);
		if (border > 0) {
			cell = handelBorder(cell, 0.1f);
		} else {
			cell.disableBorderSide(15);//全部隐藏
		}
		return cell;
	}
	
	/*
	 * 创建单元格(图片，跨列)
	 */
	public PdfPCell createCellImg(Image image, Integer rowspan, Integer border) {
		PdfPCell cell = new PdfPCell();
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setImage(image);
		if (rowspan != null && rowspan > 1) {
			cell.setRowspan(rowspan);
		}
		cell.setBorder(border);
		if (border > 0) {
			cell = handelBorder(cell, 0.1f);
		} else {
			cell.disableBorderSide(15);//全部隐藏
		}
		return cell;
	}
	
	/*
	 * 创建单元格(条形码，跨列)
	 */
	public static PdfPCell createCellBarcode(PdfWriter pdfWriter, String barcodeText, Integer rowspan, Integer colspan, Integer border) {
		PdfPCell cell = new PdfPCell();
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setPadding(20f);

	    Barcode128 code128 = new Barcode128();
	    code128.setCode(barcodeText);
	    code128.setCodeType(Barcode128.CODE128);
	    code128.setBarHeight(30f);
	    Image code128Image = code128.createImageWithBarcode(pdfWriter.getDirectContent(), null, null);
	    code128Image.setAlignment(Image.ALIGN_CENTER);
	    code128Image.scalePercent(20);
		cell.setImage(code128Image);
		if (rowspan != null && rowspan > 1) {
			cell.setRowspan(rowspan);
		}
		if (colspan != null && colspan > 1) {
			cell.setColspan(colspan);
		}
		
		cell.setBorder(border);
		if (border > 0) {
			cell = handelBorder(cell, 0.1f);
		} else {
			cell.disableBorderSide(15);//全部隐藏
		}
		return cell;
	}

	/**
	 * 创建单元格(指定字体、水平对齐、合并行单元格)
	 */
	public PdfPCell createCellText(String value, Font font, Integer horizontalAlignment, Integer colspan, Integer border) {
		PdfPCell cell = new PdfPCell();
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(horizontalAlignment);
		cell.setPhrase(new Phrase(value, font));
		cell.setColspan(colspan);
		cell.setBorder(border);
		if (border > 0) {
			cell = handelBorder(cell, 0.1f);
		} else {
			cell.disableBorderSide(15);//全部隐藏
		}
		return cell;
	}
	
	public static PdfPCell handelBorder(PdfPCell cell, float borderWidth) {
        cell.setBorder(0);
        cell.setBorderWidthTop(borderWidth);
        cell.setBorderWidthBottom(borderWidth);
        cell.setBorderWidthLeft(borderWidth);
        cell.setBorderWidthRight(borderWidth);
        cell.setBorderColorBottom(BaseColor.BLACK);
        cell.setBorderColorLeft(BaseColor.BLACK);
        cell.setBorderColorRight(BaseColor.BLACK);
        cell.setBorderColorTop(BaseColor.BLACK);
		return cell;
	}

}
